<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="字典名称" prop="dictName">
        <a-input v-model="form.dictName" placeholder="请输入字典名称，仅支持中文"/>
      </a-form-model-item>
      <a-form-model-item label="字典类型" prop="dictType">
        <a-input v-model="form.dictType" placeholder="请输入字典类型，仅支持英文、数字和下划线"
                 @input="validateDictType"/>
      </a-form-model-item>
      <a-form-model-item :label="$t('common.status')" prop="status">
        <a-select placeholder="请选择" v-model="form.status">
          <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">
            {{ d.dictLabel }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item :label="$t('common.remark')" prop="remark">
        <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear/>
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>

import {addType, getType, updateType} from '@/api/system/dict/type'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    }
  },
  components: {},
  data() {
    return {
      loading: false,
      formTitle: '',
      // 表单参数
      form: {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: '0',
        remark: undefined
      },
      open: false,
      rules: {
        dictName: [
          {required: true, message: '字典名称不能为空', trigger: 'blur'},
          {validator: this.validateChinese, trigger: 'blur'}
        ],
        dictType: [
          {required: true, message: '字典类型不能为空', trigger: 'blur'},
          {validator: this.validateEnglish, trigger: 'blur'}
        ]
      }
    }
  },
  filters: {},
  created() {
  },
  computed: {},
  watch: {},
  methods: {
    // 验证中文输入
    validateChinese(rule, value, callback) {
      if (!value) {
        callback()
        return
      }
      const chineseRegex = /^[\u4e00-\u9fa5]+$/
      if (!chineseRegex.test(value)) {
        callback(new Error('字典名称只能包含中文字符'))
      } else {
        callback()
      }
    },
    // 验证英文、数字、下划线输入
    validateEnglish(rule, value, callback) {
      if (!value) {
        callback()
        return
      }
      const englishRegex = /^[a-zA-Z0-9_]+$/
      if (!englishRegex.test(value)) {
        callback(new Error('字典类型只能包含英文、数字和下划线'))
      } else {
        callback()
      }
    },
    // 实时过滤字典类型输入（只允许英文、数字、下划线）
    validateDictType(event) {
      const value = event.target.value
      const englishRegex = /^[a-zA-Z0-9_]*$/
      if (!englishRegex.test(value)) {
        // 过滤掉非英文字符、数字、下划线
        const filteredValue = value.replace(/[^a-zA-Z0-9_]/g, '')
        this.form.dictType = filteredValue
        this.$nextTick(() => {
          event.target.value = filteredValue
        })
      }
    },
    onClose() {
      this.open = false
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictType: undefined,
        status: '0',
        remark: undefined
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.formTitle = '添加字典类型'
    },
    /** 修改按钮操作 */
    handleUpdate(row, ids) {
      this.reset()
      const dictId = row ? row.dictId : ids
      getType(dictId).then(response => {
        this.form = response.data
        this.open = true
        this.formTitle = '修改字典类型'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.dictId !== undefined) {
            updateType(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          } else {
            addType(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
